import { baseInfo } from './consts.js';

export const api = {
    baseUrl: '/api/v5/repos/{owner}/{repo}/git/trees/{sha}',
}

export const dealUrl = (url) => {
    // console.log(`prepare dealUrl: ${url}`)
    if (url.indexOf('http') === 0) {
        return url;
    }
    return baseInfo.baseDomain + url;
}

export const get = (url, params) => {
    return fetch(dealUrl(url), params).then(v => v.json())
}

export const setParamToApi = function (api, param) {
    Object.keys(param).forEach(key => {
        // console.log(`prepare replace ${key} -> ${param[key]}`)
        let matchReg = new RegExp(`\\{${key}\\}`, "g");
        api = api.replace(matchReg, param[key])
    })
    // console.log("生成 API:", api);
    return api;
}

export default {
    get,
    api,
    setParamToApi,
}
